<template>
	<view style="width: 100%;height: 100vh;background-color: #f5f5f5;">
		<view class="navbar">
					<uni-icons class="back-icon" type="left" size="36rpx" color="#00CC71" @click="navigateBack" />
					<text class="page-title">选择科室</text>
				</view>
		<view class="container">
			<view class="form-item">
				<text class="label">身高（cm）</text>
				<input class="input" v-model="formData.height" placeholder="输入身高信息" />
			</view>

			<view class="form-item">
				<text class="label">体重（kg）</text>
				<input class="input" v-model="formData.weight" placeholder="请输入体重" />
			</view>

			<view class="form-item">
				<text class="label">血压（mmHg）</text>
				<input class="input" v-model="formData.blood_ressure" placeholder="血压信息" />
			</view>

			<view class="form-item">
				<text class="label">血糖（mmol/L）</text>
				<input class="input" v-model="formData.blood_sugar" placeholder="血糖信息" />
			</view>

			<button class="submit-btn" @click="submitForm">确认更新</button>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref, onMounted } from 'vue';
	// import navVue from '../../components/nav.vue';

	const title = ref('更新健康数据')
	const formData = ref({
		height: '',
		weight: '',
		blood_ressure: '',
		blood_sugar: ''
	})

	// 获取传递过来的数据
	onMounted(() => {
		const pages = getCurrentPages()
		const currentPage = pages[pages.length - 1]
		const options = currentPage.options

		if (options.data) {
			try {
				const healthData = JSON.parse(decodeURIComponent(options.data))
				formData.value = {
					height: healthData.height || '',
					weight: healthData.weight || '',
					blood_ressure: healthData.blood_ressure || '',
					blood_sugar: healthData.blood_sugar || ''
				}
			} catch (e) {
				console.error('解析健康数据失败', e)
			}
		}
	})

	const submitForm = () => {
		const token = uni.getStorageSync('token')
		uni.request({
			url: 'http://localhost:3010/user/update_health',
			method: 'POST',
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				'Authorization': `Bearer ${token}`,
			},
			data: {
				height: formData.value.height,
				weight: formData.value.weight,
				blood_ressure: formData.value.blood_ressure,
				blood_sugar: formData.value.blood_sugar
			},
			success: (res) => {
				console.log('更新成功:', res)
				// 显示成功提示
				uni.showToast({
					title: '更新成功',
					icon: 'success'
				});
				// 延迟返回让用户看到提示
				setTimeout(() => {
					uni.navigateBack()
				}, 1500);
			},
			fail: (err) => {
				uni.showToast({
					title: '更新失败',
					icon: 'error'
				});
			}
		})
	}
	const navigateBack = () => {
	  uni.navigateBack()
	}
</script>

<style lang="scss">
	.container {
		padding: 20px;
	}
	.navbar {
		display: flex;
		align-items: center;
		height: 88rpx;
		background: #FFFFFF;
		padding: 0 24rpx;
		border-bottom: 1px solid #ECECEC;
		margin-top: 90rpx;
	
		.back-icon {
			margin-right: 16rpx;
		}
	
		.page-title {
			font-size: 36rpx;
			color: #00CC71;
			font-weight: 700;
		}
	}
	.form-item {
		margin-bottom: 20px;
		background-color: #fff;
		border-radius: 8px;
		padding: 15px;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	}

	.label {
		display: block;
		font-size: 16px;
		color: #333;
		margin-bottom: 10px;
		font-weight: bold;
	}

	.input {
		width: 100%;
		height: 40px;
		border: 1px solid #ddd;
		border-radius: 4px;
		padding: 0 10px;
		font-size: 14px;
		color: #666;
	}

	.submit-btn {
		margin-top: 30px;
		background-color: #07c160;
		color: white;
		border: none;
		border-radius: 8px;
		height: 48px;
		line-height: 48px;
		font-size: 16px;
	}
</style>